/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contable.presentacion;

import contable.aplicacion.FactoriaSQL;
import contable.dominio.Inventario;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author jaime
 */
public class Principal extends javax.swing.JFrame {

    private FactoriaSQL fsql = FactoriaSQL.getINSTANCIA();
    private static final Principal INSTANCIA = new Principal();
    private int registros = 0;

    /**
     * Creates new form Principal
     */
    private Principal() {
        initComponents();
        setLocationRelativeTo(null);
        new Logging(this, true).setVisible(true);
        if (!itemPeriodo.isSelected()) {
            JOptionPane.showMessageDialog(this, "Para poder comenzar a operar debe iniciar periodo contable\nSe iniciara ahora", "Iniciar periodo nuevo contable", JOptionPane.OK_OPTION);
        }
    }

    public static Principal getInstancia() {
        return INSTANCIA;
    }

    public void agregarRegistro(Date fecha, String descripcion, double valor) {
        javax.swing.table.DefaultTableModel contenido = (javax.swing.table.DefaultTableModel) tabRegistros.getModel();
      if (registros > 0 && registros < 14) {
            contenido.moveRow(0, 12, 1);
        } else {
            contenido.moveRow(0, 12, 1);
            contenido.setValueAt(null, 0, 0);
            contenido.setValueAt("", 0, 1);
            contenido.setValueAt(null, 0, 2);
            registros = 0;
        }
        tabRegistros.setValueAt(new SimpleDateFormat("dd MMMM 'de' yyyy 'a las' HH:mm:ss").format(fecha), 0, 0);
        tabRegistros.setValueAt(descripcion, 0, 1);
        tabRegistros.setValueAt(valor, 0, 2);
        registros++;
    }

    public void agregarPendiente(int tipo) {
        javax.swing.DefaultListModel<String> pendientes = (javax.swing.DefaultListModel) lstPendientes.getModel();
        String tarea = tareas(tipo);
        if (!pendientes.contains(tarea)) {
            pendientes.addElement(tarea);
        }
    }

    public void eliminarPendiente(int tipo) {
        javax.swing.DefaultListModel<String> pendientes = (javax.swing.DefaultListModel) lstPendientes.getModel();
        String tarea = tareas(tipo);
        if (!pendientes.contains(tarea)) {
            pendientes.removeElement(tipo);
        }
    }

    private String tareas(int tipo) {
        switch (tipo) {
            case 0:
                return "Pagar compra al credito";
            case 1:
                return "Cobrar venta al credito";
            case 2:
                return "pagar salarios";
            default:
                return null;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabRegistros = new javax.swing.JTable();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstPendientes = new javax.swing.JList();
        jXPanel2 = new org.jdesktop.swingx.JXPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuOperaciones = new javax.swing.JMenu();
        itemPeriodo = new javax.swing.JCheckBoxMenuItem();
        itemVenta = new javax.swing.JMenuItem();
        itemCompra = new javax.swing.JMenuItem();
        itemPlanilla = new javax.swing.JMenuItem();
        itemSalir = new javax.swing.JMenuItem();
        menuReportes = new javax.swing.JMenu();
        itemComprobacion = new javax.swing.JMenuItem();
        itemResultado = new javax.swing.JMenuItem();
        itemCapital = new javax.swing.JMenuItem();
        itemGeneral = new javax.swing.JMenuItem();
        menuCardex = new javax.swing.JMenu();
        itemPapelFotografico = new javax.swing.JMenuItem();
        itemTinta = new javax.swing.JMenuItem();
        itemEmpaque = new javax.swing.JMenuItem();
        itemFotografia = new javax.swing.JMenuItem();
        itemCamara = new javax.swing.JMenuItem();
        itemMemoria2 = new javax.swing.JMenuItem();
        itemMemoria4 = new javax.swing.JMenuItem();
        itemMemoria8 = new javax.swing.JMenuItem();
        itemMemoria16 = new javax.swing.JMenuItem();
        itemEstuche = new javax.swing.JMenuItem();
        itemMarco = new javax.swing.JMenuItem();
        itemLibro = new javax.swing.JMenuItem();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ventana principal");

        jTabbedPane1.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        jTabbedPane1.setToolTipText("");
        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabRegistros.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Fecha", "Descripción", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabRegistros.setName("tabRegistros"); // NOI18N
        jScrollPane1.setViewportView(tabRegistros);

        jTabbedPane1.addTab("Registros", jScrollPane1);

        jXPanel1.setName("jXPanel1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        lstPendientes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstPendientes.setName("lstPendientes"); // NOI18N
        jScrollPane2.setViewportView(lstPendientes);

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                .addContainerGap())
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Pendientes", jXPanel1);

        jXPanel2.setName("jXPanel2"); // NOI18N

        javax.swing.GroupLayout jXPanel2Layout = new javax.swing.GroupLayout(jXPanel2);
        jXPanel2.setLayout(jXPanel2Layout);
        jXPanel2Layout.setHorizontalGroup(
            jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 395, Short.MAX_VALUE)
        );
        jXPanel2Layout.setVerticalGroup(
            jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 252, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("tab3", jXPanel2);

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        menuOperaciones.setText("Operaciones");
        menuOperaciones.setName("menuOperaciones"); // NOI18N

        itemPeriodo.setSelected(true);
        itemPeriodo.setText(itemPeriodo.isSelected() ? "Cerrar periodo" : "Abrir periodo");
        itemPeriodo.setActionCommand("");
        itemPeriodo.setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR));
        itemPeriodo.setName("itemPeriodo"); // NOI18N
        menuOperaciones.add(itemPeriodo);

        itemVenta.setText("Registrar venta");
        itemVenta.setName("itemVenta"); // NOI18N
        itemVenta.addActionListener(formListener);
        menuOperaciones.add(itemVenta);

        itemCompra.setText("Registrar compra");
        itemCompra.setName("itemCompra"); // NOI18N
        itemCompra.addActionListener(formListener);
        menuOperaciones.add(itemCompra);

        itemPlanilla.setText("Registrar planilla");
        itemPlanilla.setName("itemPlanilla"); // NOI18N
        itemPlanilla.addActionListener(formListener);
        menuOperaciones.add(itemPlanilla);

        itemSalir.setText("Salir");
        itemSalir.setName("itemSalir"); // NOI18N
        itemSalir.addActionListener(formListener);
        menuOperaciones.add(itemSalir);

        jMenuBar1.add(menuOperaciones);

        menuReportes.setText("Reportes");
        menuReportes.setName("menuReportes"); // NOI18N

        itemComprobacion.setText("Balance de comprobación");
        itemComprobacion.setName("itemComprobacion"); // NOI18N
        menuReportes.add(itemComprobacion);

        itemResultado.setText("Balance de resultados");
        itemResultado.setName("itemResultado"); // NOI18N
        menuReportes.add(itemResultado);

        itemCapital.setText("Balance de capital");
        itemCapital.setName("itemCapital"); // NOI18N
        menuReportes.add(itemCapital);

        itemGeneral.setText("Balance general");
        itemGeneral.setName("itemGeneral"); // NOI18N
        menuReportes.add(itemGeneral);

        menuCardex.setText("Inventarios");
        menuCardex.setName("menuCardex"); // NOI18N

        itemPapelFotografico.setText("Cardex papel fotográfico");
        itemPapelFotografico.setName("itemPapelFotografico"); // NOI18N
        itemPapelFotografico.addActionListener(formListener);
        menuCardex.add(itemPapelFotografico);

        itemTinta.setText("Cardex tinta");
        itemTinta.setToolTipText("");
        itemTinta.setName("itemTinta"); // NOI18N
        itemTinta.addActionListener(formListener);
        menuCardex.add(itemTinta);

        itemEmpaque.setText("Cardex empaque");
        itemEmpaque.setName("itemEmpaque"); // NOI18N
        itemEmpaque.addActionListener(formListener);
        menuCardex.add(itemEmpaque);

        itemFotografia.setText("Cardex fotografía");
        itemFotografia.setName("itemFotografia"); // NOI18N
        itemFotografia.addActionListener(formListener);
        menuCardex.add(itemFotografia);

        itemCamara.setText("Cardex cámara");
        itemCamara.setName("itemCamara"); // NOI18N
        itemCamara.addActionListener(formListener);
        menuCardex.add(itemCamara);

        itemMemoria2.setText("Cardex memoría USB 2Gb");
        itemMemoria2.setName("itemMemoria2"); // NOI18N
        itemMemoria2.addActionListener(formListener);
        menuCardex.add(itemMemoria2);

        itemMemoria4.setText("Cardex memoría USB 4Gb");
        itemMemoria4.setName("itemMemoria4"); // NOI18N
        itemMemoria4.addActionListener(formListener);
        menuCardex.add(itemMemoria4);

        itemMemoria8.setText("Cardex memoría USB 8Gb");
        itemMemoria8.setName("itemMemoria8"); // NOI18N
        itemMemoria8.addActionListener(formListener);
        menuCardex.add(itemMemoria8);

        itemMemoria16.setText("Cardex memoría USB 16Gb");
        itemMemoria16.setName("itemMemoria16"); // NOI18N
        itemMemoria16.addActionListener(formListener);
        menuCardex.add(itemMemoria16);

        itemEstuche.setText("Cardex estuche para cámara");
        itemEstuche.setName("itemEstuche"); // NOI18N
        itemEstuche.addActionListener(formListener);
        menuCardex.add(itemEstuche);

        itemMarco.setText("Cardex marco de fotografía");
        itemMarco.setName("itemMarco"); // NOI18N
        itemMarco.addActionListener(formListener);
        menuCardex.add(itemMarco);

        menuReportes.add(menuCardex);

        itemLibro.setText("Libro diario");
        itemLibro.setName("itemLibro"); // NOI18N
        menuReportes.add(itemLibro);

        jMenuBar1.add(menuReportes);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 279, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
        );

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == itemPapelFotografico) {
                Principal.this.itemPapelFotograficoActionPerformed(evt);
            }
            else if (evt.getSource() == itemTinta) {
                Principal.this.itemTintaActionPerformed(evt);
            }
            else if (evt.getSource() == itemEmpaque) {
                Principal.this.itemEmpaqueActionPerformed(evt);
            }
            else if (evt.getSource() == itemFotografia) {
                Principal.this.itemFotografiaActionPerformed(evt);
            }
            else if (evt.getSource() == itemCamara) {
                Principal.this.itemCamaraActionPerformed(evt);
            }
            else if (evt.getSource() == itemMemoria2) {
                Principal.this.itemMemoria2ActionPerformed(evt);
            }
            else if (evt.getSource() == itemMemoria4) {
                Principal.this.itemMemoria4ActionPerformed(evt);
            }
            else if (evt.getSource() == itemMemoria8) {
                Principal.this.itemMemoria8ActionPerformed(evt);
            }
            else if (evt.getSource() == itemMemoria16) {
                Principal.this.itemMemoria16ActionPerformed(evt);
            }
            else if (evt.getSource() == itemEstuche) {
                Principal.this.itemEstucheActionPerformed(evt);
            }
            else if (evt.getSource() == itemMarco) {
                Principal.this.itemMarcoActionPerformed(evt);
            }
            else if (evt.getSource() == itemVenta) {
                Principal.this.itemVentaActionPerformed(evt);
            }
            else if (evt.getSource() == itemCompra) {
                Principal.this.itemCompraActionPerformed(evt);
            }
            else if (evt.getSource() == itemPlanilla) {
                Principal.this.itemPlanillaActionPerformed(evt);
            }
            else if (evt.getSource() == itemSalir) {
                Principal.this.itemSalirActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void itemPapelFotograficoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemPapelFotograficoActionPerformed
        Inventario papel = new Inventario();
        fsql.rollback(papel, 0);
        new InventarioCardex(this, papel).setVisible(true);
    }//GEN-LAST:event_itemPapelFotograficoActionPerformed

    private void itemTintaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemTintaActionPerformed
        Inventario tintas = new Inventario();
        fsql.rollback(tintas, 1);
        new InventarioCardex(this, tintas).setVisible(true);
    }//GEN-LAST:event_itemTintaActionPerformed

    private void itemEmpaqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemEmpaqueActionPerformed
        Inventario empaque = new Inventario();
        fsql.rollback(empaque, 2);
        new InventarioCardex(this, empaque).setVisible(true);
    }//GEN-LAST:event_itemEmpaqueActionPerformed

    private void itemFotografiaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemFotografiaActionPerformed
        Inventario fotografia = new Inventario();
        fsql.rollback(fotografia, 3);
        new InventarioCardex(this, fotografia).setVisible(true);
    }//GEN-LAST:event_itemFotografiaActionPerformed

    private void itemCamaraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemCamaraActionPerformed
        Inventario camara = new Inventario();
        fsql.rollback(camara, 4);
        new InventarioCardex(this, camara).setVisible(true);
    }//GEN-LAST:event_itemCamaraActionPerformed

    private void itemMemoria2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemMemoria2ActionPerformed
        Inventario memoria2Gb = new Inventario();
        fsql.rollback(memoria2Gb, 5);
        new InventarioCardex(this, memoria2Gb).setVisible(true);
    }//GEN-LAST:event_itemMemoria2ActionPerformed

    private void itemMemoria4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemMemoria4ActionPerformed
        Inventario memoria4Gb = new Inventario();
        fsql.rollback(memoria4Gb, 6);
        new InventarioCardex(this, memoria4Gb).setVisible(true);
    }//GEN-LAST:event_itemMemoria4ActionPerformed

    private void itemMemoria8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemMemoria8ActionPerformed
        Inventario memoria8Gb = new Inventario();
        fsql.rollback(memoria8Gb, 7);
        new InventarioCardex(this, memoria8Gb).setVisible(true);
    }//GEN-LAST:event_itemMemoria8ActionPerformed

    private void itemMemoria16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemMemoria16ActionPerformed
        Inventario memoria16Gb = new Inventario();
        fsql.rollback(memoria16Gb, 8);
        new InventarioCardex(this, memoria16Gb).setVisible(true);
    }//GEN-LAST:event_itemMemoria16ActionPerformed

    private void itemEstucheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemEstucheActionPerformed
        Inventario estuche = new Inventario();
        fsql.rollback(estuche, 9);
        new InventarioCardex(this, estuche).setVisible(true);
    }//GEN-LAST:event_itemEstucheActionPerformed

    private void itemMarcoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemMarcoActionPerformed
        Inventario marco = new Inventario();
        fsql.rollback(marco, 10);
        new InventarioCardex(this, marco).setVisible(true);
    }//GEN-LAST:event_itemMarcoActionPerformed

    private void itemVentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemVentaActionPerformed
        new RegistrarMercaderia(this, true).setVisible(true);
    }//GEN-LAST:event_itemVentaActionPerformed

    private void itemCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemCompraActionPerformed
        new RegistrarMercaderia(this, false).setVisible(true);
    }//GEN-LAST:event_itemCompraActionPerformed

    private void itemPlanillaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemPlanillaActionPerformed
        new RegistrarPlanilla(this).setVisible(true);
    }//GEN-LAST:event_itemPlanillaActionPerformed

    private void itemSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemSalirActionPerformed
        System.exit(0);
    }//GEN-LAST:event_itemSalirActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem itemCamara;
    private javax.swing.JMenuItem itemCapital;
    private javax.swing.JMenuItem itemCompra;
    private javax.swing.JMenuItem itemComprobacion;
    private javax.swing.JMenuItem itemEmpaque;
    private javax.swing.JMenuItem itemEstuche;
    private javax.swing.JMenuItem itemFotografia;
    private javax.swing.JMenuItem itemGeneral;
    private javax.swing.JMenuItem itemLibro;
    private javax.swing.JMenuItem itemMarco;
    private javax.swing.JMenuItem itemMemoria16;
    private javax.swing.JMenuItem itemMemoria2;
    private javax.swing.JMenuItem itemMemoria4;
    private javax.swing.JMenuItem itemMemoria8;
    private javax.swing.JMenuItem itemPapelFotografico;
    private javax.swing.JCheckBoxMenuItem itemPeriodo;
    private javax.swing.JMenuItem itemPlanilla;
    private javax.swing.JMenuItem itemResultado;
    private javax.swing.JMenuItem itemSalir;
    private javax.swing.JMenuItem itemTinta;
    private javax.swing.JMenuItem itemVenta;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXPanel jXPanel2;
    private javax.swing.JList lstPendientes;
    private javax.swing.JMenu menuCardex;
    private javax.swing.JMenu menuOperaciones;
    private javax.swing.JMenu menuReportes;
    private javax.swing.JTable tabRegistros;
    // End of variables declaration//GEN-END:variables
}
